﻿using System;
using System.Collections.Generic;
using System.Linq;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.Domain.IN;
using HiStaff.Domain.Sys;
using HiStaff.Util;
using HiStaff.Dal;
using Janus.Windows.GridEX;
using System.Data;
using System.Windows.Forms;
using HiStaff.Domain.HU;

namespace HiStaff.iProfile
{
    public partial class Welfare : FormBase
    {
        public Welfare()
        {
            InitializeComponent();
        }
        protected override bool FormLoad()
        {
            try
            {

                AddBasicCommandBar();
                
                return base.FormLoad();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }
        private void BindData()
        {
            BindDataEmp();
        }
        private void trvOrg_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            try
            {
                BindData();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }
        protected override bool AddNewButtonClick()
        {
            if (!(grdEmpList.SelectedItems.Count > 0 && grdEmpList.SelectedItems[0].RowType == RowType.Record))
            {
                MessageBox.Show(GetMessageById("MSG_NOCHOISE_EMPLOYEE"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }
        protected override bool SaveButtonClick()
        {
            try
            {
                var objSave = new HU_WELFARE();
                ValueControlsToObject(objSave, this);
                objSave.FAMILYID = ((HU_FAMILY)hiMultiColumnComboBox1.SelectedItem).ID;
                objSave.EMPLOYEEID = ((HU_EMPLOYEE)grdEmpList.SelectedItems[0].GetRow().DataRow).ID;
                OracleHelper.ExcuteNonQuery("APRU_HU_WELFARE", objSave);
                DataWelFare();
                MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }
        protected override bool CancelButtonClick()
        {
            try
            {
                BindDataFromGrid();
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        protected override bool DeleteButtonClick()
        {
            if (grdMain.SelectedItems.Count > 0)
            {
                var row = grdMain.SelectedItems[0];
                if (row.RowType == Janus.Windows.GridEX.RowType.Record)
                {
                    if (OracleHelper.ExcuteNonQuery("APRD_HU_WELFARE", grdMain.SelectedItems[0].GetRow().DataRow))
                    {
                        MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        DataWelFare();
                        return true;
                    }
                }
            }
            return false;
        }

        private void DataWelFare()
        {
            try
            {
                decimal? i = ConvertHelper.ToDecimal(grdEmpList.GetValue("ID"));
                var lstEmployee = OracleHelper.ExcuteSelectMultiObject<HU_WELFARE>("APRS_HU_WELFARE", new EMPLOYEE_SEARCH { EMPLOYEEID = i });
                grdMain.DataSource = lstEmployee;
                grdMain.Refresh();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }
        //select nhan vien
        private void BindDataEmp()
        {
            try
            {
                var lstEmployee = OracleHelper.ExcuteSelectMultiObject<HU_EMPLOYEE>("PRS_HU_EMPLOYEE", new EMPLOYEE_SEARCH { ORGID = trvOrg.OrgId, TERMINATE = chkTerminate.Value });
                grdEmpList.DataSource = lstEmployee;
                grdEmpList.Refresh();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void grdEmpList_SelectionChanged(object sender, EventArgs e)
        {
            
            try
            {
                if (grdEmpList.SelectedItems.Count > 0 && grdEmpList.SelectedItems[0].RowType == RowType.Record)
                {
                    var lstData = OracleHelper.ExcuteSelectMultiObject<HU_FAMILY>("APRS_HU_FAMILY", new HU_FAMILY
                    {
                        EMPLOYEEID = ((HU_EMPLOYEE)grdEmpList.SelectedItems[0].GetRow().DataRow).ID
                    });
                    hiMultiColumnComboBox1.DisplayMember = "FAMILYNAME";
                    hiMultiColumnComboBox1.ValueMember = "ID";
                    hiMultiColumnComboBox1.DataSource = lstData;
                    hiMultiColumnComboBox1.Refresh();
                    DataWelFare();
                }
                else
                {
                    grdMain.DataSource = null;
                    grdMain.Refresh();
                    ResetInputControl(this);

                    hiMultiColumnComboBox1.DataSource = null;
                    hiMultiColumnComboBox1.Refresh();
                }
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }
        private void BindDataFromGrid()
        {
            try
            {
                BindDataFromGridToForm(grdMain, this);
                hiMultiColumnComboBox1.SelectedIndex = -1;
                if (grdMain.SelectedItems.Count > 0 && grdMain.SelectedItems[0].RowType == RowType.Record)
                {
                    for (int i = 0; i < ((List<HU_FAMILY>)hiMultiColumnComboBox1.DataSource).Count; i++)
                    {
                        if (((List<HU_FAMILY>)hiMultiColumnComboBox1.DataSource)[i].ID ==
                            ((HU_WELFARE)grdMain.SelectedItems[0].GetRow().DataRow).FAMILYID)
                        {
                            hiMultiColumnComboBox1.SelectedIndex = i;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex) { WriteErrorLog(ex); }
        }
        private void grdMain_SelectionChanged(object sender, EventArgs e)
        {
            BindDataFromGrid();
        }

        private void chkTerminate_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                BindData();
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

        private void hiDatePicker3_Validated(object sender, EventArgs e)
        {
            try
            {
                if (!Common.CheckEffectDateAndExpireDate(dtpDateFrom, dtpToDate))
                {
                    dtpToDate.Focus();
                    MessageBox.Show(GetMessageById("MSG_INVALID_DATEPAYMENT"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
            }
            catch(Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

    }
}
